package zhongbo.java.air.airflightpath.listener;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;
import zhongbo.java.air.airflightpath.config.RedisListenerConfig;
import zhongbo.java.air.airflightpath.model.AirFlightPath;
import zhongbo.java.air.airflightpath.service.IAirFightPathService;
import zhongbo.java.air.common.vo.PageVo;

import java.util.Date;

@Component
@Slf4j
public class RedisLogSubscribeLogChanelListener implements MessageListener {

    @Autowired
    private IAirFightPathService<AirFlightPath> airFightPathService;

    @Autowired
    private RedisLogChanelPublisher redisLogChanelPublisher;

    @Override
    public void onMessage(Message message, byte[] bytes) {
        String channel = new String(message.getChannel()); // 获取频道名称
        String msg = new String(message.getBody());
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        log.info("接收到频道 [{}] Json航迹数据消息: {}", channel, msg);
        AirFlightPath airFlightPath = parseAirFlightPath(msg);
        AirFlightPath insert = new AirFlightPath();
        if (airFlightPath != null) {
            log.info("接收到频道 [{}] 解析后的航迹数据: {}", channel, airFlightPath);
            insert = airFightPathService.insert(airFlightPath);
        }
//        PageVo<AirFlightPath> airFlightPathPageVo = airFightPathService.queryByPage(1, 10, airFlightPath.getIcaoCode(), airFlightPath.getTimestamp(), airFlightPath.getTimestamp());

        // 转换为json格式的数据
        String jsonResult = JSONUtil.toJsonStr(insert);
        redisLogChanelPublisher.publishMessage(RedisListenerConfig.LIVE_FLIGHT_PATH_ZHU,jsonResult);
    }

    public AirFlightPath parseAirFlightPath(String msg) {
        if (msg == null){
            throw new RuntimeException("航迹数据为空");
        }
        AirFlightPath airFlightPath = new AirFlightPath();
        if (msg.startsWith("\"") && msg.endsWith("\"")) { // 处理带引号的消息
            msg = msg.substring(1, msg.length() - 1); // 去除开头和结尾的引号
        }
        msg = msg.replace("\\", "");
        JSONObject jsonObject = JSONUtil.parseObj(msg);
        if (jsonObject.getStr("icao_code") == null){
            airFlightPath.setIcaoCode("UNKNOWN");
        }else {
            airFlightPath.setIcaoCode(jsonObject.getStr("icao_code"));
        }
        String timestampStr = jsonObject.getStr("timestamp");
        Date timestamp = DateUtil.parse(timestampStr, "yyyy-MM-dd HH:mm:ss.SSS"); // 显式指定格式
        airFlightPath.setTimestamp(timestamp);

        // 验证毫秒值
        log.info("解析后的毫秒时间戳：{}", timestamp.getTime()); // 输出 long 类型的毫秒
        log.info("格式化后的时间：{}", DateUtil.format(timestamp, "yyyy-MM-dd HH:mm:ss.SSS")); // Hutool 格式化
        airFlightPath.setLatitude(jsonObject.getDouble("latitude"));
        airFlightPath.setLongitude(jsonObject.getDouble("longitude"));
        airFlightPath.setAltitude(jsonObject.getInt("altitude"));
        airFlightPath.setSpeed(jsonObject.getInt("speed"));
        airFlightPath.setHeading(jsonObject.getInt("heading"));
        if (jsonObject.getStr("source_type") == null){
            airFlightPath.setSourceType("");
        }else {
            airFlightPath.setSourceType(jsonObject.getStr("source_type"));
        }

        return airFlightPath;
    }
}